package communication.common.command;

import io.vertx.core.cli.Argument;
import io.vertx.core.cli.CLI;
import io.vertx.core.cli.CommandLine;
import io.vertx.core.cli.Option;
import io.vertx.ext.shell.command.CommandBuilder;
import io.vertx.ext.shell.command.CommandProcess;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * 用户信息相关命令
 * @author mac
 */
@Slf4j
@Data
@RequiredArgsConstructor
public class UserCommandCli implements BaseCommandCli{
    @Override
    public CommandBuilder getCliBuilder() {
        // 命令例子：
        CLI user = CLI.create("user")
                .addOption(new Option().setShortName("c").setLongName("create").setDescription("创建账户")).addArgument(new Argument().setArgName("name")).addArgument(new Argument().setArgName("passwd"))
                .addOption(new Option().setShortName("login").setLongName("login").setDescription("用户登录"));

        CommandBuilder commandBuilder=CommandBuilder.command(user);
        commandBuilder.processHandler(process -> {
            CommandLine commandLine = process.commandLine();
            // 创建用户
            createUser(commandLine,process);

            // 登录


        });


        return commandBuilder;
    }

    /**
     * create user
     * @param commandLine
     * @param process
     */
    private void createUser(CommandLine commandLine, CommandProcess process) {
        String create = commandLine.getOptionValue("create");
        String name = commandLine.getArgumentValue("name");
        String passwd = commandLine.getArgumentValue("passwd");
        if(!StringUtils.isEmpty(create)){
            if(StringUtils.isEmpty(name.trim())){
                // todo : 优化底层socket连接的请求处理方式
//                process.write("can not ")
            }

        }

    }
}
